package com.huadao.cart_ccs.experiment.log.environmentLog;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.experiment.log.baseLoglist.BaseLogListPresenter;
import com.huadao.cart_ccs.experiment.log.baseLoglist.IBaseLogListContract;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.entity.EnvironmentParams;
import com.huadao.cart_ccs.greendao.gen.EnvironmentParamsDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class EnvironmentLogPresenter extends BaseLogListPresenter {

    private EnvironmentParamsDao environmentParamsDao;

    public EnvironmentLogPresenter(IBaseLogListContract.IBaseLogListView baseLogListView) {
        super(baseLogListView);
        environmentParamsDao=GreenDaoManager.getInstance().getNoCacheSession().getEnvironmentParamsDao();
    }

    @Override
    public void getDataList() {
        getData_dispose=Observable
                .create(new ObservableOnSubscribe<List<EnvironmentParams>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<EnvironmentParams>> emitter) throws Exception {
                        skip=0;
                        QueryBuilder queryBuilder=environmentParamsDao.queryBuilder().where(EnvironmentParamsDao.Properties.ProjectId.eq(projectId),EnvironmentParamsDao.Properties.StageId.eq(stageId)).orderDesc(EnvironmentParamsDao.Properties.Date).limit(count);
                        if(!condition.equals("全部")){
                            byte type=getTypeByStr(condition);
                            if(type!=-1) {
                                queryBuilder.where(EnvironmentParamsDao.Properties.Type.eq(type));
                            }
                        }
                        List<EnvironmentParams> environmentParamsList=queryBuilder.list();
                        skip+=environmentParamsList.size();
                        emitter.onNext(environmentParamsList);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<EnvironmentParams>>() {
                    @Override
                    public void accept(List<EnvironmentParams> o) throws Exception {
                        baseView.showDataList(o);
                    }
                });
    }

    public byte getTypeByStr(String typeStr){
        if(typeStr.equals(baseView.getContext().getString(R.string.CO2))){
            return EnvironmentParams.TYPE_CO2;
        }else if(typeStr.equals(baseView.getContext().getString(R.string.O2))){
            return EnvironmentParams.TYPE_O2;
        }else if(typeStr.equals(baseView.getContext().getString(R.string.temperature))){
            return EnvironmentParams.TYPE_Temperature;
        }else {
            return -1;
        }
    }

    @Override
    public void getMoreData() {
        getData_dispose=Observable
                .create(new ObservableOnSubscribe<List<EnvironmentParams>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<EnvironmentParams>> emitter) throws Exception {
                        QueryBuilder queryBuilder=environmentParamsDao.queryBuilder().where(EnvironmentParamsDao.Properties.ProjectId.eq(projectId),EnvironmentParamsDao.Properties.StageId.eq(stageId)).orderDesc(EnvironmentParamsDao.Properties.Date).offset(skip).limit(count);
                        if(!condition.equals("全部")){
                            byte type=getTypeByStr(condition);
                            if(type!=-1) {
                                queryBuilder.where(EnvironmentParamsDao.Properties.Type.eq(type));
                            }
                        }
                        List<EnvironmentParams> environmentParamsList=queryBuilder.list();
                        skip+=environmentParamsList.size();
                        emitter.onNext(environmentParamsList);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<EnvironmentParams>>() {
                    @Override
                    public void accept(List<EnvironmentParams> o) throws Exception {
                        baseView.showMoreDataList(o);
                    }
                });
    }
}
